import Base from '../../base.js';
import contentmodel from '../../../model/content';
export default class extends Base {
    /**获取内容列表1 */
    async indexAction() {
        const contentdb = think.model('content') as contentmodel;
        let uidsArr: string[] = []
        let cidsArr: string[] = []
        let orderStr: string = ''
        let page: number = this.get('page')
        let uids: string = this.get('uids')
        let cids: string = this.get('cids')
        let praise: number = this.get('praise')
        let collect: number = this.get('collect')
        let comment: number = this.get('comment')
        let currentnum: number = this.get('num') !== undefined ?  this.get('num') : 10 
        let _complex = {}
        try {
            // 0 降序  1 升序
            if (praise !== undefined) {
                let flag: string = praise == 1 ? 'c.praise ASC' : 'c.praise DESC'
                orderStr += flag + ','
            }
            if (collect !== undefined) {
                let flag: string = praise == 1 ? 'c.collect ASC' : 'c.collect DESC'
                orderStr += flag + ','
            }
            if (comment !== undefined) {
                let flag: string = praise == 1 ? 'c.comment ASC' : 'c.comment DESC'
                orderStr += flag + ','
            }    
            if (uids !== undefined) {
                uidsArr = uids.split(",")
                _complex = { [`u.id`]: ['IN', uids]}
            }else if (cids !== undefined) {
                cidsArr = cids.split(",")
                _complex = { [`cl.id`]: ['IN', cids]}
            }else if(cids !== undefined && uids !== undefined){
                _complex = {[`u.id`]: ['IN', uids],[`cl.id`]: ['IN', cids]}
            }else{
                _complex = "1=1"
            }
            orderStr = orderStr.substring(0, orderStr.length - 1)    
            const data = await contentdb.getPageContent(page, uidsArr, cidsArr, orderStr, currentnum,_complex)
            return this.json(this.dataJsonSuccess(data))
        } catch (error) {
            return this.json(this.dataJsonError(500, null))
        }

    }
}
